package com.hsgene.product.service.impl;

import com.hsgene.common.DeleteFlagType;
import com.hsgene.common.util.qiniu.QiniuCertificateUtil;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.generator.dto.GeneratorDto;
import com.hsgene.product.condition.BannerCondition;
import com.hsgene.product.domain.GeneticBanners;
import com.hsgene.product.dto.GeneticBannersDto;
import com.hsgene.product.exception.ProductErrorCode;
import com.hsgene.product.persistence.GeneticBannersMapper;
import com.hsgene.product.service.BannerService;
import com.hsgene.product.service.api.GeneratorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author: fuxiang
 * @date: 2018/10/12 14:28
 */
@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    GeneticBannersMapper geneticBannersMapper;
    @Resource
    GeneratorService generatorService;

    @Override
    public ApiResult<GeneticBanners> addBanner(GeneticBanners geneticBanners) {
        ApiResult<GeneratorDto> generator = generatorService.getGenerator(null, null);
        if (!generator.isSucc()) {
            return ApiResult.fail(generator.getError());
        }
        if (geneticBanners.getType() == 2) {
            GeneticBanners geneticBannersBySort = geneticBannersMapper.getBySortAndType(geneticBanners);
            if (geneticBannersBySort != null) {
                return ApiResult.fail(ProductErrorCode.THE_SORT_VALUE_ALREADY_EXISTS);
            }
        }
        geneticBanners.setId(generator.getData().getSerialNumber());
        geneticBanners.setCreateDateTime(new Date());
        geneticBanners.setUpdateDateTime(geneticBanners.getCreateDateTime());
        geneticBanners.setDeleteFlag(DeleteFlagType.NOT_DELETED.getCode());
        geneticBannersMapper.insertSelective(geneticBanners);
        return ApiResult.succ(geneticBanners);
    }

    @Override
    public ApiResult updateBanner(GeneticBanners geneticBanners) {
        geneticBanners.setUpdateDateTime(new Date());
        geneticBannersMapper.updateByPrimaryKeySelective(geneticBanners);
        return ApiResult.succ();
    }

    @Override
    public ApiResult getPage(BannerCondition condition) {
        List<GeneticBanners> bannersList = geneticBannersMapper.getPage(condition);
        List<GeneticBannersDto> collect = bannersList.stream().map(c -> {
            GeneticBannersDto geneticBannersDto = new GeneticBannersDto();
            BeanUtils.copyProperties(c, geneticBannersDto);
            String downloadCertificate =  QiniuCertificateUtil.getDownloadCertificateStr(c.getImageUrl());
            geneticBannersDto.setImageUrl(downloadCertificate);
            return geneticBannersDto;
        }).collect(Collectors.toList());
        return ApiResult.succ(collect);
    }

    @Override
    public ApiResult deleteMore(List<GeneticBanners> condition) {
        geneticBannersMapper.deleteMore(condition);
        return ApiResult.succ();
    }

    @Override
    public ApiResult deleteBanner(String id) {
        GeneticBanners geneticBanners = new GeneticBanners();
        geneticBanners.setId(id);
        geneticBanners.setUpdateDateTime(new Date());
        geneticBanners.setDeleteFlag(DeleteFlagType.DELETE.getCode());
        geneticBannersMapper.updateByPrimaryKeySelective(geneticBanners);
        return ApiResult.succ();
    }

}
